<template>
  <view class="tree-content">
    <view
      class="value"
      :class="!value.length && 'placeholder'"
      @click="showTree ? close() : show()"
      >{{ value.length ? value[0].label : '请选择' }}</view
    >
    <next-tree
      ref="nextTreeRef"
      v-bind="$attrs"
      :treeData="treeData"
      @confirm="nextTreeConfirm"
    />
  </view>
</template>

<script>
  import * as Utils from '@/utils/index.js';

  export default {
    name: 'tree',
    components: {},
    props: {
      treeData: {
        type: Array,
        require: true,
      },
      checkedItem: {
        type: Array,
        default: () => [],
      },
    },
    data() {
      return {
        value: [],
        showTree: false,
      };
    },
    mounted() {
      if (this.checkedItem.length) this.value = this.checkedItem;
    },
    methods: {
      nextTreeConfirm: function (list) {
        // console.log('选中项的数量列表list：', list)
        this.value = list;
        this.$emit('change', list);
      },
      show() {
        this.$refs.nextTreeRef._show();
      },
      hide() {
        this.$refs.nextTreeRef._hide();
      },
    },
  };
</script>

<style lang="scss" scoped>
  .tree-content {
    width: 100%;
    height: 100%;
    display: flex;
    box-sizing: border-box;
    flex-direction: row;
    align-items: center;
    border: 1px solid #dcdfe6;
    border-radius: 4px;
  }

  .value {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    padding-left: 10px;
  }
  .placeholder {
    color: #999;
    font-size: 12px;
  }
</style>
